package pers.agony.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.agony.dao.CheckGroupDao;
import pers.agony.dao.SetmealDao;
import pers.agony.entity.PageResult;
import pers.agony.entity.QueryPageBean;
import pers.agony.exception.MyException;
import pers.agony.pojo.CheckGroup;
import pers.agony.pojo.Setmeal;
import pers.agony.service.SetmealService;

import java.util.List;

/**
 * @author : Agony
 * @date : Created in 3:49 下午 2021/11/3
 * @Description:
 * @Modified By:
 * @version: : 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //对querypagebean中的querystring做非空判断，拼接字符串
        if(queryPageBean.getQueryString()!=null){
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }
        //调用pagehelper的start方法
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //调用dao层findByCondition方法查询得page对象
        Page page = setmealDao.findByCondition(queryPageBean.getQueryString());
        //调用page对象的gettotal和getresult封装到pageresult进行返回
        long total = page.getTotal();
        List<CheckGroup> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Transactional
    @Override
    public void add(int[] checkgroupIds, Setmeal setmeal) {
        //先添加setmeal表单数据
        setmealDao.add(setmeal);
        //获得id
        Integer id = setmeal.getId();
        //遍历
        if(checkgroupIds!=null) {
            for (int checkgroupId : checkgroupIds) {
                setmealDao.addSetmealCheckGroup(id, checkgroupId);
            }
        }
    }

    @Override
    public Setmeal findOne(int id) {
        return setmealDao.findOne(id);
    }

    @Override
    public List<Integer> findsetmealCheckGroupBysetmealId(int id) {
        return setmealDao.findsetmealCheckGroupBysetmealId(id);
    }

    @Transactional
    @Override
    public void update(Setmeal setmeal, int[] checkgroupIds) {
        //调用dao层方法更新检查组信息
        setmealDao.update(setmeal);
        //获取setmeal的id
        Integer setmealId = setmeal.getId();
        //根据id删除checkgroup和setmeal关系列表中的数据
        setmealDao.deleteSetmealCheckGroup(setmealId);
        //遍历checkgroupIds数组，将元素与setmeal关联添加到数据库表单中
        for (int checkGroupId : checkgroupIds) {
            setmealDao.addSetmealCheckGroup(setmealId,checkGroupId);
        }
    }

    @Transactional
    @Override
    public void delete(int id) {
        int count = checkGroupDao.countOfSetmealByCheckGroupId(id);
        if(count>0){
            MyException myException = new MyException("该检查组已存在于套餐中，不能删除！");
        }else{
            setmealDao.deleteSetmealCheckGroup(id);
            setmealDao.deleteSetmeal(id);
        }
    }
}
